<?php
require_once(dirname(__FILE__) . '/PortalUtil.class.php');

class Node extends PortalUtil {
	public $tbl = Constant::tbl_wifi_node;

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	function _where($request) {
		$username_s = $request['username_s'];
	
		$where = " WHERE 1=1";
		
		if (!empty($username_s)) {
			$where .= " AND username = '" . $username_s . "'";
		}
	
		// $where .= $this->_where_default($request);
		// $where = $this->_where_aa($where);
	
		return $where;
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function layout($controller, $request) {
		$this->pretreat($controller);
		
		if (empty($_SESSION['username'])) {
			$controller->display($request, 'wlan/login');
			return true;
		}
		
		$request['username_s'] = $_SESSION['username'];
		
		$this->nav['home'] = true;
		
		$this->findPara($controller, $request);
		
		// $this->orderby_s = "id";
		
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$pager_option = array (
			'db' => $db,
			'sql' => $sql,
			'PageSize' => 8,
			'CurrentPageID' => $request['p'],
			'numItems' => $request['n']
		);
		
		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();
		
		if ($pager->number > 0) {
		
			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);
		
				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}
		
		$controller->display($request, 'mac/index');
		return true;
	}
	
	function _update($request) {
		
		$code = $request['gw_id'];
		
		$content = json_encode($request);
		
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET content = '%s', modifytime = NOW() WHERE code = '%s'";
		$sql = sprintf($fmt, $this->tbl, $content, $code);
		$db->query($sql);
		
		$url = "http://fy.tecmold.net/t/wlan/?do=Node.update";
		// $url = "http://localhost/t/wlan/?do=Node.online";
		
		$ret = $this->http_post_data($url, $content);
	}
}
?>
